<template>
    <div class="portal-detail-container form-list-wrap" v-loading="loading">
        <app-cate v-model="form.app_id" v-if="appCateShow"></app-cate>
        <div class="table-wrapper">
            <div class="query-search-thin" style="margin-bottom: 10px;">
                <el-form :inline="true" size="mini">
                    <el-form-item>
                        <el-input placeholder="搜索报表名称" v-model="form.title"
                                  class="input-with-select" clearable
                                  @clear="refresh" @keyup.enter.native="refresh">
                            <el-button slot="append" icon="el-icon-search" @click="refresh()"></el-button>
                        </el-input>
                    </el-form-item>
                </el-form>
                <el-button type="primary" @click="handleShowCreate()" size="mini" icon="el-icon-plus">创建报表</el-button>
            </div>
            <div class="m-table-container" ref="tableContainer">
                <el-table :data="list" class="m-table" border size="mini" style="width: 100%" :height="tableHeight">
                    <el-table-column
                        prop="title"
                        label="报表名">
                        <template slot-scope="scope">
                            {{ scope.row.title }}
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="form.form_name"
                        label="表单名"
                        width="260">
                    </el-table-column>
                    <el-table-column
                            prop="create_time"
                            label="创建时间"
                            align="center"
                            width="160">
                    </el-table-column>

                    <el-table-column label="操作" width="220" align="center">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="text"
                                @click="handleShowDatas(scope.row, scope.$index)">数据</el-button>
                            <el-button
                                    size="mini"
                                    type="text"
                                    @click="handleView(scope.row, scope.$index)">查看</el-button>
                            <el-button
                                size="mini"
                                type="text"
                                @click="handleEdit(scope.row, scope.$index)">编辑</el-button>
                            <el-button
                                size="mini"
                                type="text"
                                @click="handleDelete(scope.row, scope.$index)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <resize-observer @notify="handleResize" />
            </div>
            <el-pagination
                    @size-change="refresh"
                    @current-change="refresh"
                    :current-page.sync="pagination.page"
                    :page-sizes="[10, 20, 50, 100]"
                    :page-size.sync="pagination.pageSize"
                    layout="total, sizes, prev, pager, next"
                    :total="pagination.total">
            </el-pagination>
        </div>

        <el-dialog :title="winName" :visible.sync="showCreate" top="60px" :append-to-body="true"
                   :fullscreen="false" :before-close="closeWin" custom-class="m-dialog-fullscreen">
            <c-define @close="closeWin" :mod-id="currentReport && currentReport.reportdef_id"
                      :show.sync="showCreate" :is-view="isView"></c-define>
        </el-dialog>

        <el-dialog :title="datasWinName" :visible.sync="datasWinShow" top="60px" :append-to-body="true"
                   :fullscreen="false">
            <c-report-datas @close="closeDatasWin"
                            :report-define-id="currentDatasReport && currentDatasReport.reportdef_id"
                            :show.sync="datasWinShow"></c-report-datas>
        </el-dialog>
    </div>
</template>

<script>
    import { mapState, mapActions } from 'vuex';
    import cDefine from './define';
    import cReportDatas from './datas';

    export default {
        props: {
            appCateShow: {
                default: true
            }
        },
        data() {
            return {
                loading: false,
                showCreate: false,
                isView: false,
                currentModuleId: -1,
                // modReportId: null,
                currentReport: null,
                pagination: {
                    page: 1,
                    pageSize: 50,
                    total: 0
                },
                tableHeight: 500,

                form: {
                    app_id: -1,
                    title: ''
                },

                datasWinName: '',
                datasWinShow: false,
                currentDatasReport: null
            }
        },
        components: {
            cDefine,
            cReportDatas
        },
        created: function () {
            // this.pid = this.$route.query.pid;
        },
        mounted: function () {
            // this.groupId = this.$route.params.groupId;
            // this.init();
            this.refresh();
        },
        watch: {
            '$route' (to, from) {
                // this.groupId = this.$route.params.groupId;
                // this.refresh();
                console.log('=++++++++++++++++++++-------- route ...');
            },
            'form.app_id'() {
                this.refresh()
            }
        },
        computed: {
            ...mapState([

            ]),
            winName() {
                let typeName = '';

                if(this.isView) {
                    typeName = '查看';
                } else {
                    if(this.currentReport && this.currentReport.reportdef_id) {
                        typeName = '修改';
                    } else {
                        typeName = '创建';
                    }
                }

                if(this.currentReport && this.currentReport.title) {
                    return typeName + `【${this.currentReport.title}】`;
                }
                return typeName
            }
        },
        /*beforeRouteEnter(to, from, next) {
            next(vm => {

            });
        },*/
        methods: {
            init() {
                this.$nextTick(() => {
                    this.handleResize();
                });
            },
            handleResize() {
                let rect = this.$refs.tableContainer.getBoundingClientRect();
                let height = _.get(rect, 'height');

                if(height > 0) {
                    this.tableHeight = height;
                }
                // console.log(rect);
            },
            async refresh() {
                let ret = await this.$japi.report.getReportDefList(this.form, this.pagination);
                this.list = _.get(ret, 'rows', []);
                this.pagination.total = _.get(ret, 'count', 0);
            },
            closeWin(flag) {
                this.showCreate = false;
                this.isView = false;
                this.refresh();
            },
            closeDatasWin() {
                this.datasWinShow = false;
            },
            handleShowDatas(it) {
                this.datasWinShow = true;
                this.currentDatasReport = it;
                this.datasWinName = `【${it.title}】的报表数据记录`;
            },
            handleView(it) {
                this.showCreate = true;
                this.isView = true;
                this.currentReport = it;
            },
            handleEdit(it) {
                this.showCreate = true;
                this.isView = false;
                this.currentReport = it;
            },
            async _delete(id) {
                let ret = await this.$japi.report.removeReportDef(id);

                if(ret) {
                    this.refresh()
                }
            },
            handleDelete(it) {

                if(!it || !it.reportdef_id) {
                    return;
                }
                this.$confirm(`此操作将永久删除该报表定义【${it.title}】, 是否继续?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    await this._delete(it.reportdef_id);
                }).catch(() => {
                    /*this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });*/
                });
            },
            handleShowCreate() {
                this.showCreate = true;
                this.isView = false;
                this.currentReport = null;
            },
        }
    }
</script>

<style>

</style>
